/*
 * INET		An implementation of the TCP/IP protocol suite for the LINUX
 *		operating system.  INET is implemented using the  BSD Socket
 *		interface as the means of communication with the user level.
 *
 *		The Internet Protocol (IP) module.
 *
 * Version:	@(#)ip.c	1.0.16b	9/1/93
 *
 * Authors:	Ross Biro, <bir7@leland.Stanford.Edu>
 *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
 *		Donald Becker, <becker@super.org>
 *		Alan Cox, <gw4pts@gw4pts.ampr.org>
 *		Richard Underwood
 *		Stefan Becker, <stefanb@yello.ping.de>
 *		Jorge Cwik, <jorge@laser.satlink.net>
 *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
 *		
 *
 * Fixes:
 *		Alan Cox	:	Commented a couple of minor bits of surplus code
 *		Alan Cox	:	Undefining IP_FORWARD doesn't include the code
 *					(just stops a compiler warning).
 *		Alan Cox	:	Frames with >=MAX_ROUTE record routes, strict routes or loose routes
 *					are junked rather than corrupting things.
 *		Alan Cox	:	Frames to bad broadcast subnets are dumped
 *					We used to process them non broadcast and
 *					boy could that cause havoc.
 *		Alan Cox	:	ip_forward sets the free flag on the
 *					new frame it queues. Still crap because
 *					it copies the frame but at least it
 *					doesn't eat memory too.
 *		Alan Cox	:	Generic queue code and memory fixes.
 *		Fred Van Kempen :	IP fragment support (borrowed from NET2E)
 *		Gerhard Koerting:	Forward fragmented frames correctly.
 *		Gerhard Koerting: 	Fixes to my fix of the above 8-).
 *		Gerhard Koerting:	IP interface addressing fix.
 *		Linus Torvalds	:	More robustness checks
 *		Alan Cox	:	Even more checks: Still not as robust as it ought to be
 *		Alan Cox	:	Save IP header pointer for later
 *		Alan Cox	:	ip option setting
 *		Alan Cox	:	Use ip_tos/ip_ttl settings
 *		Alan Cox	:	Fragmentation bogosity removed
 *					(Thanks to Mark.Bush@prg.ox.ac.uk)
 *		Dmitry Gorodchanin :	Send of a raw packet crash fix.
 *		Alan Cox	:	Silly ip bug when an overlength
 *					fragment turns up. Now frees the
 *					queue.
 *		Linus Torvalds/ :	Memory leakage on fragmentation
 *		Alan Cox	:	handling.
 *		Gerhard Koerting:	Forwarding uses IP priority hints
 *		Teemu Rantanen	:	Fragment problems.
 *		Alan Cox	:	General cleanup, comments and reformat
 *		Alan Cox	:	SNMP statistics
 *		Alan Cox	:	BSD address rule semantics. Also see
 *					UDP as there is a nasty checksum issue
 *					if you do things the wrong way.
 *		Alan Cox	:	Always defrag, moved IP_FORWARD to the config.in file
 *		Alan Cox	: 	IP options adjust sk->priority.
 *		Pedro Roque	:	Fix mtu/length error in ip_forward.
 *		Alan Cox	:	Avoid ip_chk_addr when possible.
 *	Richard Underwood	:	IP multicasting.
 *		Alan Cox	:	Cleaned up multicast handlers.
 *		Alan Cox	:	RAW sockets demultiplex in the BSD style.
 *		Gunther Mayer	:	Fix the SNMP reporting typo
 *		Alan Cox	:	Always in group 224.0.0.1
 *	Pauline Middelink	:	Fast ip_checksum update when forwarding
 *					Masquerading support.
 *		Alan Cox	:	Multicast loopback error for 224.0.0.1
 *		Alan Cox	:	IP_MULTICAST_LOOP option.
 *		Alan Cox	:	Use notifiers.
 *		Bjorn Ekwall	:	Removed ip_csum (from slhc.c too)
 *		Bjorn Ekwall	:	Moved ip_fast_csum to ip.h (inline!)
 *		Stefan Becker   :       Send out ICMP HOST REDIRECT
 *	Arnt Gulbrandsen	:	ip_build_xmit
 *		Alan Cox	:	Per socket routing cache
 *		Alan Cox	:	Fixed routing cache, added header cache.
 *		Alan Cox	:	Loopback didnt work right in original ip_build_xmit - fixed it.
 *		Alan Cox	:	Only send ICMP_REDIRECT if src/dest are the same net.
 *		Alan Cox	:	Incoming IP option handling.
 *		Alan Cox	:	Set saddr on raw output frames as per BSD.
 *		Alan Cox	:	Stopped broadcast source route explosions.
 *		Alan Cox	:	Can disable source routing
 *
 *  
 *
 * To Fix:
 *		IP option processing is mostly not needed. ip_forward needs to know about routing rules
 *		and time stamp but that's about all. Use the route mtu field here too
 *		IP fragmentation wants rewriting cleanly. The RFC815 algorithm is much more efficient
 *		and could be made very efficient with the addition of some virtual memory hacks to permit
 *		the allocation of a buffer that can then be 'grown' by twiddling page tables.
 *		Output fragmentation wants updating along with the buffer management to use a single 
 *		interleaved copy algorithm so that fragmenting has a one copy overhead. Actual packet
 *		output should probably do its own fragmentation at the UDP/RAW layer. TCP shouldn't cause
 *		fragmentation anyway.
 *
 *		FIXME: copy frag 0 iph to qp->iph
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 */

#include <asm/segment.h>
#include <asm/system.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/config.h>

#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/in.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>

#include <net/snmp.h>
#include <net/ip.h>
#include <net/protocol.h>
#include <net/route.h>
#include <net/tcp.h>
#include <net/udp.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <net/arp.h>
#include <net/icmp.h>
#include <net/raw.h>
#include <net/checksum.h>
#include <linux/igmp.h>
#include <linux/ip_fw.h>

#define CONFIG_IP_DEFRAG

extern int last_retran;
extern void sort_send(struct sock *sk);

#define min(a,b)	((a)<(b)?(a):(b))
#define LOOPBACK(x)	(((x) & htonl(0xff000000)) == htonl(0x7f000000))

/*
 *	SNMP management statistics
 */

#ifdef CONFIG_IP_FORWARD
struct ip_mib ip_statistics={1,64,};	/* Forwarding=Yes, Default TTL=64 */
#else
struct ip_mib ip_statistics={0,64,};	/* Forwarding=No, Default TTL=64 */
#endif

/*
 *	Handle the issuing of an ioctl() request
 *	for the ip device. This is scheduled to
 *	disappear
 */

int ip_ioctl(struct sock *sk, int cmd, unsigned long arg)
{
	switch(cmd)
	{
		default:
			return(-EINVAL);
	}
}


/*
 *	Take an skb, and fill in the MAC header.
 */
//ip层对创建以太网头的封装，函数中调用了dev->hard_header函数调用
//@skb:skb结构体
//@daddr:路由后的路由地址
//@len:当eth->hard_header函数的type参数为ETH_P_802_3时，使用此参数
//@dev:网卡设备
//@saddr:此函数中为用到此参数
//返回值：以太网头长度或0(没有以太网构建函数时返回0)
static int ip_send(struct sk_buff *skb, unsigned long daddr, int len, struct device *dev, unsigned long saddr)
{
	int mac = 0;

	skb->dev = dev;
	skb->arp = 1;//先将skb->arp置1，当dev->hard_header返回负值时，再将其设置为0。
	if (dev->hard_header)//如果eth->hard_header有值时，调用
	{
		/*
		 *	Build a hardware header. Source address is our mac, destination unknown
		 *  	(rebuild header will sort this out)
		 */
		//调用dev->hard_header函数，参数daddr和saddr(注意：两个是mac地址)都传入NULL，saddr时dev的mac地址
		//如果dev是lo设备时，那以太网头中目的地址将被设置为0，因为时发送给自己，所以
		//不再需要进行arp操作，此时返回正的以太网头长度。如果dev不是lo设备，那构建以太网
		//头中缺失目的地址，此时返回负的以太网头长度
		mac = dev->hard_header(skb->data, dev, ETH_P_IP, NULL, NULL, len, skb);
		if (mac < 0) //如果返回负值之下以下操作
		{
			mac = -mac;//mac变为正值
			skb->arp = 0;//skb->arp再设置为0，因为以太网头未创建成功，在调用dev_queue_xmit函数中会重新创建
			skb->raddr = daddr;	/* next routing address */
		}
	}

	//到此，无论是创建以太网头成功或失败，mac都是正的以太网头长度。创建以太网头的成功与否在skb->arp中已经做了记录。
	return mac;
}

int ip_id_count = 0;

/*
 * This routine builds the appropriate hardware/IP headers for
 * the routine.  It assumes that if *dev != NULL then the
 * protocol knows what it's doing, otherwise it uses the
 * routing/ARP tables to select a device struct.
 */
//ip头创建
//@skb:
//@saddr:源ip地址
//@daddr:目的ip地址
//@dev:网卡设备，二级指针，未传入网卡设备，则返回路由到的网卡设备
int ip_build_header(struct sk_buff *skb, unsigned long saddr, unsigned long daddr,
		struct device **dev, int type, struct options *opt, int len, int tos, int ttl)
{
	struct rtable *rt;
	unsigned char *buff;
	unsigned long raddr;
	int tmp;
	unsigned long src;
	struct iphdr *iph;

	buff = skb->data;//buff指向skb->data

	/*
	 *	See if we need to look up the device.
	 */

#ifdef CONFIG_INET_MULTICAST	
	if(MULTICAST(daddr) && *dev==NULL && skb->sk && *skb->sk->ip_mc_name)
		*dev=dev_get(skb->sk->ip_mc_name);
#endif
	if (*dev == NULL)//未传入网卡设备，
	{
		if(skb->localroute)
			rt = ip_rt_local(daddr, NULL, &src);//本地路由，查找路由表
		else
			rt = ip_rt_route(daddr, NULL, &src);//非本地路由，插在路由表
		if (rt == NULL)
		{
			ip_statistics.IpOutNoRoutes++;
			return(-ENETUNREACH);
		}

		*dev = rt->rt_dev;//*dev赋值为路由表中记录的网卡设备
		/*
		 *	If the frame is from us and going off machine it MUST MUST MUST
		 *	have the output device ip address and never the loopback
		 */
		if (LOOPBACK(saddr) && !LOOPBACK(daddr))//如果源ip地址是127.0.0.1且目的ip地址不是
			saddr = src;/*rt->rt_dev->pa_addr;*///
		raddr = rt->rt_gateway;//路由地址设置为路由表网关

	}
	else
	{
		/*
		 *	We still need the address of the first hop.
		 */
		if(skb->localroute)
			rt = ip_rt_local(daddr, NULL, &src);
		else
			rt = ip_rt_route(daddr, NULL, &src);
		/*
		 *	If the frame is from us and going off machine it MUST MUST MUST
		 *	have the output device ip address and never the loopback
		 */
		if (LOOPBACK(saddr) && !LOOPBACK(daddr))
			saddr = src;/*rt->rt_dev->pa_addr;*/

		raddr = (rt == NULL) ? 0 : rt->rt_gateway;//
	}

	/*
	 *	No source addr so make it our addr
	 */
	if (saddr == 0)
		saddr = src;

	/*
	 *	No gateway so aim at the real destination
	 */
	if (raddr == 0)
		raddr = daddr;

	/*
	 *	Now build the MAC header.
	 */

	tmp = ip_send(skb, raddr, len, *dev, saddr);//调用ip层封装的以太网头构建函数，进行以太网头构建
	buff += tmp;//buff向后以动以太网头长度字节，指向ip头开始位置
	len -= tmp;//此操作无用，下面未使用

	/*
	 *	Book keeping
	 */
	//此处skb记录dev，源ip地址
	skb->dev = *dev;
	skb->saddr = saddr;
	//如果skb->sk有值，设置skb->sk的saddr成员
	if (skb->sk)
		skb->sk->saddr = saddr;

	/*
	 *	Now build the IP header.
	 */

	/*
	 *	If we are using IPPROTO_RAW, then we don't need an IP header, since
	 *	one is being supplied to us by the user
	 */
	//如果type是原始套结字，返回tmp，tmp中存放这以太网头长度，即ip_send的返回值
	if(type == IPPROTO_RAW)
		return (tmp);

	/*
	 *	Build the IP addresses
	 */
	//ip头构建
	iph=(struct iphdr *)buff;
	
	iph->version  = 4;
	iph->tos      = tos;
	iph->frag_off = 0;
	iph->ttl      = ttl;
	iph->daddr    = daddr;
	iph->saddr    = saddr;
	iph->protocol = type;
	iph->ihl      = 5;
	skb->ip_hdr   = iph;
	
	//如果不是原始套结字，则返回ip头加以太网头长度和
	return(20 + tmp);	/* IP header plus MAC header size */
}


/*
 *	Generate a checksum for an outgoing IP datagram.
 */
//ip头校验和计算
void ip_send_check(struct iphdr *iph)
{
	iph->check = 0;
	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
}

/************************ Fragment Handlers From NET2E **********************************/


/*
 *	This fragment handler is a bit of a heap. On the other hand it works quite
 *	happily and handles things quite well.
 */
//ip分片队列，所有接收到的ip包都先存在此队列中
/****************************************************
struct ipq {
  unsigned char		*mac;		// pointer to MAC header 以太网头，重组时用到
  struct iphdr	*iph;		// pointer to IP header ip头，当收到一个ip包时，用这里的和新收到的里面的源地址、目的地址、协议加id来匹配属不属于同一片
  int		len;		// total length of original datagram  整个未分片时的ip包长度，在重组时用此长度来分配空间
  short			ihlen;		// length of the IP header  ip头长度
  short 	maclen;		// length of the MAC header   mac长度
  struct timer_list timer;	// when will this queue expire?
  struct ipfrag		*fragments;	// linked list of received fragments 收到的同一个ip分片放在此连表中，如果当前ipq在ipqueue的尾部，则next=NULL
  struct ipq	*next;		// linked list pointers  指向ip分片队列下一个
  struct ipq	*prev;      // 指向ip分片队列上一个，如果当前ipq在ipqueue的头部，则prev=NULL
  struct device *dev;		// Device - for icmp replies
};

//分片结构体
struct ipfrag {
  int		offset;		// offset of fragment in IP datagram   片偏移
  int		end;		// last byte of data in datagram   下一片开始，在作所有片是否全部接收完成检测时用到此成员
  int		len;		// length of this fragment  本片长度
  struct sk_buff *skb;			// complete received fragment   收到的当前片的skb
  unsigned char		*ptr;		// pointer into real fragment data	 skb中data成员中，除了ip头之后的数据
  struct ipfrag		*next;		// linked list pointers  //链入所属的ipq中的fragments队列中，根据offset插入对应的位置
  struct ipfrag		*prev;
};
*****************************************************/
static struct ipq *ipqueue = NULL;		/* IP fragment queue	*///分配队列全局变量

/*
 *	Create a new fragment entry.
 */
//当收到一个ip分片包时，调用此函数创建一个ipfrag分片结构体，将此结构图存入所属的ipq链表中，位置和offset的偏移是一一对应的
// @offset:片偏移
// @end:下一片开始
// @skb:接收到的skb
// @ptr:skb中data偏移ip头后的数据，也就是ip包的数据部分
//
// 返回值:分配并初始化好的ipfrag结构体
static struct ipfrag *ip_frag_create(int offset, int end, struct sk_buff *skb, unsigned char *ptr)
{
	struct ipfrag *fp;

	fp = (struct ipfrag *) kmalloc(sizeof(struct ipfrag), GFP_ATOMIC);
	if (fp == NULL)
	{
		NETDEBUG(printk("IP: frag_create: no memory left !\n"));
		return(NULL);
	}
	memset(fp, 0, sizeof(struct ipfrag));

	/* Fill in the structure. */
	fp->offset = offset;
	fp->end = end;
	fp->len = end - offset;
	fp->skb = skb;
	fp->ptr = ptr;

	return(fp);
}


/*
 *	Find the correct entry in the "incomplete datagrams" queue for
 *	this IP datagram, and return the queue entry address if found.
 */
//在ipqueue链表中查找刚接收到的ip包的ipq
//@iph:刚接收到的ip包的ip头
//
//返回值:查找到的ipq
static struct ipq *ip_find(struct iphdr *iph)
{
	struct ipq *qp;
	struct ipq *qplast;

	cli();
	qplast = NULL;
	//遍历ipqueue。判断是否属于同一个ip包的依据：同一个id、相同的源ip地址、相同的目的ip地址、相同的协议
	for(qp = ipqueue; qp != NULL; qplast = qp, qp = qp->next)
	{
		if (iph->id== qp->iph->id && iph->saddr == qp->iph->saddr &&
			iph->daddr == qp->iph->daddr && iph->protocol == qp->iph->protocol)
		{
			del_timer(&qp->timer);	/* So it doesn't vanish on us. The timer will be reset anyway */
			sti();
			return(qp);
		}
	}
	sti();
	return(NULL);
}


/*
 *	Remove an entry from the "incomplete datagrams" queue, either
 *	because we completed, reassembled and processed it, or because
 *	it timed out.
 */
//释放一个ipq
static void ip_free(struct ipq *qp)
{
	struct ipfrag *fp;
	struct ipfrag *xp;

	/*
	 * Stop the timer for this entry.
	 */

	del_timer(&qp->timer);

	/* Remove this entry from the "incomplete datagrams" queue. */
	cli();
	//将ipq从ipqueue队列上摘除
	if (qp->prev == NULL)
	{
		ipqueue = qp->next;
		if (ipqueue != NULL)
			ipqueue->prev = NULL;
	}
	else
	{
		qp->prev->next = qp->next;
		if (qp->next != NULL)
			qp->next->prev = qp->prev;
	}

	/* Release all fragment data. */
	//将ipq中ipfrag分片释放掉
	fp = qp->fragments;
	while (fp != NULL)
	{
		xp = fp->next;
		IS_SKB(fp->skb);
		kfree_skb(fp->skb,FREE_READ);
		kfree_s(fp, sizeof(struct ipfrag));
		fp = xp;
	}
	
	//释放掉以太网头
	/* Release the MAC header. */
	kfree_s(qp->mac, qp->maclen);
	
	//释放掉ipq中的iph
	/* Release the IP header. */
	kfree_s(qp->iph, 64 + 8);
	//释放ipq
	/* Finally, release the queue descriptor itself. */
	kfree_s(qp, sizeof(struct ipq));
	sti();
}


/*
 *	Oops- a fragment queue timed out.  Kill it and send an ICMP reply.
 */
//如果在规定时间内未收所有被分片了的ip包，将发送一个icmp差错包给对方，并释放掉ipq
static void ip_expire(unsigned long arg)
{
	struct ipq *qp;

	qp = (struct ipq *)arg;

	/*
	 *	Send an ICMP "Fragment Reassembly Timeout" message.
	 */

	ip_statistics.IpReasmTimeout++;
	ip_statistics.IpReasmFails++;   
	/* This if is always true... shrug */
	if(qp->fragments!=NULL)
		icmp_send(qp->fragments->skb,ICMP_TIME_EXCEEDED,
				ICMP_EXC_FRAGTIME, 0, qp->dev);

	/*
	 *	Nuke the fragment queue.
	 */
	ip_free(qp);
}


/*
 * 	Add an entry to the 'ipq' queue for a newly received IP datagram.
 * 	We will (hopefully :-) receive all other fragments of this datagram
 * 	in time, so we just create a queue for this datagram, in which we
 * 	will insert the received fragments at their respective positions.
 */
//ipq创建，当收到一个新的ip分片包时，创建一个新的ipq
//并初始化其中的成员
static struct ipq *ip_create(struct sk_buff *skb, struct iphdr *iph, struct device *dev)
{
	struct ipq *qp;
	int maclen;
	int ihlen;

	qp = (struct ipq *) kmalloc(sizeof(struct ipq), GFP_ATOMIC);
	if (qp == NULL)
	{
		NETDEBUG(printk("IP: create: no memory left !\n"));
		return(NULL);
		skb->dev = qp->dev;
	}
	memset(qp, 0, sizeof(struct ipq));

	/*
	 *	Allocate memory for the MAC header.
	 *
	 *	FIXME: We have a maximum MAC address size limit and define
	 *	elsewhere. We should use it here and avoid the 3 kmalloc() calls
	 */
	//计算以太网头长度
	maclen = ((unsigned long) iph) - ((unsigned long) skb->data);
	//ipq的mac成员分配空间
	qp->mac = (unsigned char *) kmalloc(maclen, GFP_ATOMIC);
	if (qp->mac == NULL)
	{
		NETDEBUG(printk("IP: create: no memory left !\n"));
		kfree_s(qp, sizeof(struct ipq));
		return(NULL);
	}

	/*
	 *	Allocate memory for the IP header (plus 8 octets for ICMP).
	 */
	//ip头长度计算
	ihlen = (iph->ihl * sizeof(unsigned long));
	//ipq的iph成员分配空间
	qp->iph = (struct iphdr *) kmalloc(64 + 8, GFP_ATOMIC);
	if (qp->iph == NULL)
	{
		NETDEBUG(printk("IP: create: no memory left !\n"));
		kfree_s(qp->mac, maclen);
		kfree_s(qp, sizeof(struct ipq));
		return(NULL);
	}

	/* Fill in the structure. */
	memcpy(qp->mac, skb->data, maclen);//初始化mac成员，即将以太网头拷贝到此成员中
	memcpy(qp->iph, iph, ihlen + 8); //初始化iph成员
	qp->len = 0;//记录所有ip分片的长度，随着该ip分片的收到，此值一直在增加
	qp->ihlen = ihlen;//ip头长度
	qp->maclen = maclen;//以太网头长度
	qp->fragments = NULL;
	qp->dev = dev;//接收该ip分片的网卡设备

	/* Start a timer for this entry. */
	//定时器，超时时间30秒，防止ip分片攻击，或ip分配在网络中丢失
	qp->timer.expires = IP_FRAG_TIME;		/* about 30 seconds	*/
	qp->timer.data = (unsigned long) qp;		/* pointer to queue	*/
	qp->timer.function = ip_expire;			/* expire function	*/
	add_timer(&qp->timer);

	/* Add this entry to the queue. */
	//加入到iqueue队列中，头加入法
	qp->prev = NULL;
	cli();
	qp->next = ipqueue;
	if (qp->next != NULL)
		qp->next->prev = qp;
	ipqueue = qp;
	sti();
	return(qp);
}


/*
 *	See if a fragment queue is complete.
 */
//判断所有的ip分片是否接收完成
//ipfrag的end成员存放的是下一个分片的偏移
//
//返回值：1：接收完成  0：未接收完成
static int ip_done(struct ipq *qp)
{
	struct ipfrag *fp;
	int offset;

	/* Only possible if we received the final fragment. */
	if (qp->len == 0)
		return(0);

	/* Check all fragment offsets to see if they connect. */
	fp = qp->fragments;
	offset = 0;//初始化为0，第一个分片偏移（ipfrag->offset）就是0，所以临时变量offset初始化为0，下面第一次循环
	//比较即为两个值的比较，之后offset被初始化为了下一片的偏移，再比较，如果ipfrag->offset和offset不一样，则未
	//接收完成
	while (fp != NULL)
	{
		if (fp->offset > offset) //
			return(0);	/* fragment(s) missing */
		offset = fp->end;
		fp = fp->next;
	}

	/* All fragments are present. */
	return(1);
}


/*
 *	Build a new IP datagram from all its fragments.
 *
 *	FIXME: We copy here because we lack an effective way of handling lists
 *	of bits on input. Until the new skb data handling is in I'm not going
 *	to touch this with a bargepole. This also causes a 4Kish limit on
 *	packet sizes.
 */
//当所有收到的分片进行组合，把收到的多个skb组合成一个skb
static struct sk_buff *ip_glue(struct ipq *qp)
{
	struct sk_buff *skb;
	struct iphdr *iph;
	struct ipfrag *fp;
	unsigned char *ptr;
	int count, len;

	/*
	 *	Allocate a new buffer for the datagram.
	 */
	//计算需要分配空间的长度，qp->maclen：以太网头长度，qp->ihlen：ip头长度，qp->len：数据长度，上面讲此结构体时说过此成员，注意，qp->len只是个预估的长度，真实的长度要小于此长度，而且下面还会再统计，count即是统计出来的真实长度
	//然后将count赋值给ip头里的tot_len
	len = qp->maclen + qp->ihlen + qp->len;
	//分配skb
	if ((skb = alloc_skb(len,GFP_ATOMIC)) == NULL)
	{
		ip_statistics.IpReasmFails++;
		NETDEBUG(printk("IP: queue_glue: no memory for gluing queue 0x%X\n", (int) qp));
		ip_free(qp);
		return(NULL);
	}
	//开始初始化新skb
	/* Fill in the basic details. */
	skb->len = (len - qp->maclen);
	skb->h.raw = skb->data;//skb->h.raw指向skb->data
	skb->free = 1;

	/* Copy the original MAC and IP headers into the new buffer. */
	ptr = (unsigned char *) skb->h.raw;//临时变量ptr也指向skb->data，下以行向里面拷贝以太网头
	memcpy(ptr, ((unsigned char *) qp->mac), qp->maclen);//拷贝以太网头
	ptr += qp->maclen;//ptr指针向后移动以太网头长度，下一行拷贝ip头
	memcpy(ptr, ((unsigned char *) qp->iph), qp->ihlen);//拷贝ip头
	ptr += qp->ihlen;//ptr指针继续向后移动ip头长度，为下面拷贝数据做准备
	skb->h.raw += qp->maclen;//此时skb->h.raw指向ip头部

	count = 0;//用于统计真实数据长度。

	/* Copy the data portions of all fragments into the new buffer. */
	fp = qp->fragments;//取出第一个分片
	while(fp != NULL)//开始循环，将所有分片中（ipfrag->ptr）数据拷贝到新skb的data中
	{
		if(count+fp->len > skb->len)//判断真实数据长度是否比skb->len小，如果大于的话就出错了
		{
			NETDEBUG(printk("Invalid fragment list: Fragment over size.\n"));
			ip_free(qp);
			kfree_skb(skb,FREE_WRITE);
			ip_statistics.IpReasmFails++;
			return NULL;
		}
		memcpy((ptr + fp->offset), fp->ptr, fp->len);//将当前分片的数据拷贝到新skb的data中组合成完成的数据
		count += fp->len;//真实数据统计
		fp = fp->next;//指向下一个分片
	}

	/* We glued together all fragments, so remove the queue entry. */
	ip_free(qp);

	/* Done with all fragments. Fixup the new IP header. */
	iph = skb->h.iph;//skb->h.iph和skb->h.raw属于共用体，所以是一个地址，赋值给指针iph，进行里面数值的修正
	iph->frag_off = 0;//此成员包含3位标志加偏移，整体置为0，保留位0,DF(不分片)0，MF(更多分片)0，片偏移0
	iph->tot_len = htons((iph->ihl * sizeof(unsigned long)) + count);//tot_len长度为数据真实长度count加头长度，单位是字节
	skb->ip_hdr = iph;

	ip_statistics.IpReasmOKs++;
	return(skb);
}


/*
 *	Process an incoming IP datagram fragment.
 */
//当ip层收到一个被分片的包时调用此函数,未分片（mf标志和offset都为0即为未分片）不会调用。
//@iph：收到的ip头
//@skb：接收到的skb
//@dev：接收此skb的网卡设备
//返回值：如果所有分片都接收完成，组合成一个skb并返回，如果未接收完将如参skb添加到其所属的ipq中返回NULL
static struct sk_buff *ip_defrag(struct iphdr *iph, struct sk_buff *skb, struct device *dev)
{
	struct ipfrag *prev, *next, *tmp;
	struct ipfrag *tfp;
	struct ipq *qp;
	struct sk_buff *skb2;
	unsigned char *ptr;
	int flags, offset;
	int i, ihl, end;

	ip_statistics.IpReasmReqds++;

	/* Find the entry of this IP datagram in the "incomplete datagrams" queue. */
	qp = ip_find(iph);//通过iph查找ipq

	/* Is this a non-fragmented datagram? */
	//取出offset和三位标志
	offset = ntohs(iph->frag_off);
	flags = offset & ~IP_OFFSET;//取出三为标志
	offset &= IP_OFFSET;//取出offset
	if (((flags & IP_MF) == 0) && (offset == 0))//如果mf和offset都为0，此ip包是未进行分片的，未进行分片的ip包不应该走到此函数中，返回即可
	{
		if (qp != NULL) //未分片的包如果找到了iqp，那这有点蹊跷
			ip_free(qp);	/* Huh? How could this exist?? */
		return(skb);//将skb返回
	}

	offset <<= 3;		/* offset is in 8-byte chunks *///右移3位也就是乘于8，获取到真正的偏移

	/*
	 * If the queue already existed, keep restarting its timer as long
	 * as we still are receiving fragments.  Otherwise, create a fresh
	 * queue entry.
	 */

	if (qp != NULL)//如果找到，更新定时器
	{
		del_timer(&qp->timer);
		qp->timer.expires = IP_FRAG_TIME;	/* about 30 seconds */
		qp->timer.data = (unsigned long) qp;	/* pointer to queue */
		qp->timer.function = ip_expire;		/* expire function */
		add_timer(&qp->timer);
	}
	else //位知道，创建一个新的ipq，稍后将创建一个新的ipfrag，让后将ipfrag加入到ipq的fragments队列中
	{
		/*
		 *	If we failed to create it, then discard the frame
		 */
		if ((qp = ip_create(skb, iph, dev)) == NULL)
		{
			skb->sk = NULL;
			kfree_skb(skb, FREE_READ);
			ip_statistics.IpReasmFails++;
			return NULL;
		}
	}

	/*
	 *	Determine the position of this fragment.
	 */
	//计算头长度
	ihl = (iph->ihl * sizeof(unsigned long));
	//计算下一分片的起始值
	end = offset + ntohs(iph->tot_len) - ihl;

	/*
	 *	Point into the IP datagram 'data' part.
	 */
	//获取此ip包的数据部分
	ptr = skb->data + dev->hard_header_len + ihl;

	/*
	 *	Is this the final fragment?
	 */
	//当mf标志为0时，说明收到了最后一个分片（注意：收到最后一个分片部代表所有分片都收到了，1.ip_build_xmit函数发送时是从最后一个分片发送的；2.每个分片途径的路由可能不同，到达的先后顺序也就不一样了），那就把end当作ipq中len的长度
	if ((flags & IP_MF) == 0)
		qp->len = end;

	/*
	 * 	Find out which fragments are in front and at the back of us
	 * 	in the chain of fragments so far.  We must know where to put
	 * 	this fragment, right?
	 */
	//开始查找接收到的分片在iqp->fragments中的位置，根据offset比较来查找
	prev = NULL;
	for(next = qp->fragments; next != NULL; next = next->next)
	{
		if (next->offset > offset)
			break;	/* bingo! */
		prev = next;
	}

	/*
	 * 	We found where to put this one.
	 * 	Check for overlap with preceding fragment, and, if needed,
	 * 	align things so that any overlaps are eliminated.
	 */
	//网络攻击中有一种攻击叫ip分片重叠攻击，发送ip分片时，分片的偏移和他前面或后面的分片是重叠的，这样
	//就会造成真实数据比分配的空间要大，重组时就会内存越界，导致内核中发生段错误，致使内核崩溃
	//1.判断前一片是否有重叠。offset < prev->end这个判断看着不舒服，换个位置prev->end > offset，这样就
	//直观了，前面分片的end成员是下一片的开始，正常情况应该是小于或者等于offset，但现在大于了，说明发生
	//了重叠。处理方法是将offset向后偏移重叠差值，然后数据部分也往后偏移重叠差值，至于数据的合法性，交由
	//上层去处理，ip层不作任何处理
	if (prev != NULL && offset < prev->end)
	{
		i = prev->end - offset;
		offset += i;	/* ptr into datagram */
		ptr += i;	/* ptr into fragment data */
	}

	/*
	 * Look for overlap with succeeding segments.
	 * If we can merge fragments, do it.
	 */
	//2.判断后面分片是否有重叠。
	for(tmp=next; tmp != NULL; tmp = tfp)
	{
		tfp = tmp->next;
		if (tmp->offset >= end)//如果tmp->offset大于或等于end（下一分片开始）
			break;		/* no overlaps at all */

		i = end - next->offset;			/* overlap is 'i' bytes */
		tmp->len -= i;				/* so reduce size of	*///更新tmp->len值减小重叠差值
		tmp->offset += i;			/* next fragment	*///tmp->offset向后偏移重叠差值
		tmp->ptr += i;                                    //tmp->ptr数据向后偏移重叠差值
		/*
		 *	If we get a frag size of <= 0, remove it and the packet
		 *	that it goes with.
		 */
		if (tmp->len <= 0)//如果tmp->len小于或等于0，那此分片出错，将其释放掉
		{
			if (tmp->prev != NULL)
				tmp->prev->next = tmp->next;
			else
				qp->fragments = tmp->next;

			if (tfp->next != NULL)
				tmp->next->prev = tmp->prev;
			
			next=tfp;	/* We have killed the original next frame */

			kfree_skb(tmp->skb,FREE_READ);
			kfree_s(tmp, sizeof(struct ipfrag));
		}
	}

	/*
	 *	Insert this fragment in the chain of fragments.
	 */
	//创建ipfrag
	tfp = NULL;
	tfp = ip_frag_create(offset, end, skb, ptr);

	/*
	 *	No memory to save the fragment - so throw the lot
	 */

	if (!tfp)
	{
		skb->sk = NULL;
		kfree_skb(skb, FREE_READ);
		return NULL;
	}
	//将创建的ipfrag加入到ipq->fragments队列的对应位置
	tfp->prev = prev;
	tfp->next = next;
	if (prev != NULL)
		prev->next = tfp;
	else
		qp->fragments = tfp;

	if (next != NULL)
		next->prev = tfp;

	/*
	 * 	OK, so we inserted this new fragment into the chain.
	 * 	Check if we now have a full IP datagram which we can
	 * 	bump up to the IP layer...
	 */
	//判断所以分片是否都接收完成，如果完成将进行分片重组
	if (ip_done(qp))
	{
		skb2 = ip_glue(qp);		/* glue together the fragments *///分片重组
		return(skb2);//将重组得到的skb返回
	}
	return(NULL);
}


/*
 *	This IP datagram is too large to be sent in one piece.  Break it up into
 *	smaller pieces (each of size equal to the MAC header plus IP header plus
 *	a block of the data of the original IP data part) that will yet fit in a
 *	single device frame, and queue such a frame for sending by calling the
 *	ip_queue_xmit().  Note that this is recursion, and bad things will happen
 *	if this function causes a loop...
 *
 *	Yes this is inefficient, feel free to submit a quicker one.
 *
 *	**Protocol Violation**
 *	We copy all the options to each fragment. !FIXME!
 */
//ip分片函数，ip_queue_xmit和ip_forward函数中调用
//@sock:sock结构体
//@skb:skb结构体
//@dev:要发送数据的网卡设备
//@is_frag:使用了低两位(bit0,bit1),当是最后一片时bit0置1，当时分配偏移offset不为0时bit1置1
//无返回值
void ip_fragment(struct sock *sk, struct sk_buff *skb, struct device *dev, int is_frag)
{
	struct iphdr *iph;
	unsigned char *raw;
	unsigned char *ptr;
	struct sk_buff *skb2;
	int left, mtu, hlen, len;
	int offset;
	unsigned long flags;

	/*
	 *	Point into the IP datagram header.
	 */
	//取出skb中数据部分，它是以太网头开始部分
	raw = skb->data;
	iph = (struct iphdr *) (raw + dev->hard_header_len);//以太网头加上以太网头长度就得到ip头开始部分

	skb->ip_hdr = iph;//将ip头赋值给skb的ip_hdr成员

	/*
	 *	Setup starting values.
	 */

	hlen = (iph->ihl * sizeof(unsigned long));//计算ip头长度
	left = ntohs(iph->tot_len) - hlen;	/* Space per frame *///计算ip报文数据长度，用ip头中tot_len减去ip头长度得到
	hlen += dev->hard_header_len;		/* Total header size *///计算以太网头和ip头总和
	mtu = (dev->mtu - hlen);		/* Size of data space *///计算以太网可以发送的数据长度，即用dev->mtu减去以太网头和ip头的总长度。这里叫净负荷payload
	ptr = (raw + hlen);			/* Where to start from *///ip报文数据开始为置

	/*
	 *	Check for any "DF" flag. [DF means do not fragment]
	 */
	//如果ip报文设置了不允许分片，那发送icmp差错报文给对端，然后返回
	if (ntohs(iph->frag_off) & IP_DF)
	{
		/*
		 *	Reply giving the MTU of the failed hop.
		 */
		ip_statistics.IpFragFails++;
		icmp_send(skb,ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, dev->mtu, dev);
		return;
	}

	/*
	 *	The protocol doesn't seem to say what to do in the case that the
	 *	frame + options doesn't fit the mtu. As it used to fall down dead
	 *	in this case we were fortunate it didn't happen
	 */
	//如果mtu小于8，那此报文错误
	if(mtu<8)
	{
		/* It's wrong but it's better than nothing */
		icmp_send(skb,ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED,dev->mtu, dev);
		ip_statistics.IpFragFails++;
		return;
	}

	/*
	 *	Fragment the datagram.
	 */

	/*
	 *	The initial offset is 0 for a complete frame. When
	 *	fragmenting fragments it's wherever this one starts.
	 */
	//如果bit1被设置了，那临时变量offset初始值被设置为ip报文中的offset。此种情况发生在ip报文是被转发是，即在ip_forward函数中调用，也就是说
	//此时该设备是太路由器或工作在路由模式，它发出去的网卡设备的mtu小于发送端的mtu，此时就要再进行分片处理
	if (is_frag & 2)
		offset = (ntohs(iph->frag_off) & 0x1fff) << 3;
	else
		offset = 0;


	/*
	 *	Keep copying data until we run out.
	 */
	//以下代码进行分片发送处理
	//left在上面被初始化为了ip报文数据长度
	while(left > 0)
	{
		len = left;//len被赋值为left
		/* IF: it doesn't fit, use 'mtu' - the data space left */
		if (len > mtu)//如果len大于mtu，那len赋值为mtu，下面分配skb用到
			len = mtu;
		/* IF: we are not sending upto and including the packet end
		   then align the next start on an eight byte boundary */
		if (len < left)//当发送的不是最后一个分片时，将此分片按8字节对齐
		{
			len/=8;
			len*=8;
		}
		/*
		 *	Allocate buffer.
		 */
		//分配skb，skb的data成员空间大小为len+hlen。
		if ((skb2 = alloc_skb(len + hlen,GFP_ATOMIC)) == NULL)
		{
			NETDEBUG(printk("IP: frag: no memory for new fragment!\n"));
			ip_statistics.IpFragFails++;
			return;
		}

		/*
		 *	Set up data on packet
		 */
		//skb2->arp=skb->arp，skb->arp记录arp是否完成
		skb2->arp = skb->arp;
		if(skb->free==0)
			printk("IP fragmenter: BUG free!=1 in fragmenter\n");
		skb2->free = 1;
		skb2->len = len + hlen;//skb2->len要发送的包（整个链路层包）长度初始化,数据长度加以太网头长度加ip头长度,在ip_queue_xmit函数中，将使用此数据减去以太网头长度去初始化ip->tot_len
		skb2->h.raw=(char *) skb2->data;
		/*
		 *	Charge the memory for the fragment to any owner
		 *	it might possess
		 */

		save_flags(flags);
		if (sk)
		{
			cli();
			sk->wmem_alloc += skb2->mem_len;//本sock缓冲区计算，每个sock的缓冲区都有限制，不能大于某个值
			skb2->sk=sk;
		}
		restore_flags(flags);
		skb2->raddr = skb->raddr;	/* For rebuild_header - must be here *///下一跳ip地址

		/*
		 *	Copy the packet header into the new buffer.
		 */

		memcpy(skb2->h.raw, raw, hlen);//构建skb2的以太网头和ip头，注意：raw指向的是skb的以太网头开始位置

		/*
		 *	Copy a block of the IP datagram.
		 */
		memcpy(skb2->h.raw + hlen, ptr, len);//复制数据部分，此处复制完了，下面会改变ptr值，使其指向下一次复制的位置
		left -= len;//left减小len长度

		skb2->h.raw+=dev->hard_header_len;//skb2->h.raw指向了本分片的ip头部

		/*
		 *	Fill in the new header fields.
		 */
		iph = (struct iphdr *)(skb2->h.raw/*+dev->hard_header_len*/);//本分片的ip头一部分数据需要作修正，
		iph->frag_off = htons((offset >> 3));//偏移的修正
		/*
		 *	Added AC : If we are fragmenting a fragment thats not the
		 *		   last fragment then keep MF on each bit
		 */
		if (left > 0 || (is_frag & 1))
			iph->frag_off |= htons(IP_MF);//more fragments标志修正
		ptr += len;//ptr位置修改，指向下一分片要复制的位置
		offset += len;//偏移后移

		/*
		 *	Put this fragment into the sending queue.
		 */

		ip_statistics.IpFragCreates++;

		ip_queue_xmit(sk, dev, skb2, 2);//提交skb2,free参数传入2
	}
	ip_statistics.IpFragOKs++;
}



#ifdef CONFIG_IP_FORWARD

/*
 *	Forward an IP datagram to its next destination.
 */
//ip包转发
//
//@is_frag:使用了低两位(bit0,bit1),当是最后一片时bit0置1，当时分配偏移offset不为0时bit1置1,bit2置1此包为NAT转发包
void ip_forward(struct sk_buff *skb, struct device *dev, int is_frag, unsigned long target_addr, int target_strict)
{
	struct device *dev2;	/* Output device */
	struct iphdr *iph;	/* Our header */
	struct sk_buff *skb2;	/* Output packet */
	struct rtable *rt;	/* Route we use */
	unsigned char *ptr;	/* Data pointer */
	unsigned long raddr;	/* Router IP address */
#ifdef CONFIG_IP_FIREWALL
	int fw_res = 0;		/* Forwarding result */	
	
	/* 
	 *	See if we are allowed to forward this.
 	 *	Note: demasqueraded fragments are always 'back'warded.
	 */

	//is_frag的bit2置1&&防火墙判断
	if(!(is_frag&4) && (fw_res=ip_fw_chk(skb->h.iph, dev, ip_fw_fwd_chain, ip_fw_fwd_policy, 0))!=1)
	{
		if(fw_res==-1)
			icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0, dev);
		return;
	}
#endif
	/*
	 *	According to the RFC, we must first decrease the TTL field. If
	 *	that reaches zero, we must reply an ICMP control message telling
	 *	that the packet's lifetime expired.
	 *
	 *	Exception:
	 *	We may not generate an ICMP for an ICMP. icmp_send does the
	 *	enforcement of this so we can forget it here. It is however
	 *	sometimes VERY important.
	 */

	iph = skb->h.iph;
	iph->ttl--;//通过一个路由器，ttl减一，当为0是，此包丢弃

	/*
	 *	Re-compute the IP header checksum.
	 *	This is inefficient. We know what has happened to the header
	 *	and could thus adjust the checksum as Phil Karn does in KA9Q
	 */
	//校验和检测，
	iph->check = ntohs(iph->check) + 0x0100;
	if ((iph->check & 0xFF00) == 0)
		iph->check++;		/* carry overflow */
	iph->check = htons(iph->check);
	//ttl减为0，包丢弃
	if (iph->ttl <= 0)
	{
		/* Tell the sender its packet died... */
		icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0, dev);
		return;
	}

	/*
	 * OK, the packet is still valid.  Fetch its destination address,
	 * and give it to the IP sender for further processing.
	 */
	//查找路由表
	rt = ip_rt_route(target_addr, NULL, NULL);
	if (rt == NULL)
	{
		/*
		 *	Tell the sender its packet cannot be delivered. Again
		 *	ICMP is screened later.
		 */
		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, 0, dev);
		return;
	}


	/*
	 * Gosh.  Not only is the packet valid; we even know how to
	 * forward it onto its final destination.  Can we say this
	 * is being plain lucky?
	 * If the router told us that there is no GW, use the dest.
	 * IP address itself- we seem to be connected directly...
	 */

	raddr = rt->rt_gateway;

	if (raddr != 0)
	{
		/*
		 *	Strict routing permits no gatewaying
		 */
		
		if(target_strict)
		{
			icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0, dev);
			kfree_skb(skb, FREE_READ);
			return;
		}
	
		/*
		 *	There is a gateway so find the correct route for it.
		 *	Gateways cannot in turn be gatewayed.
		 */
		//再次查找路由表，为什么？？？
		rt = ip_rt_route(raddr, NULL, NULL);
		if (rt == NULL)
		{
			/*
			 *	Tell the sender its packet cannot be delivered...
			 */
			icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0, dev);
			return;
		}
		if (rt->rt_gateway != 0)
			raddr = rt->rt_gateway;
	}
	else
		raddr = target_addr;

	/*
	 *	Having picked a route we can now send the frame out.
	 */
	//转发的网卡
	dev2 = rt->rt_dev;

	/*
	 *	In IP you never have to forward a frame on the interface that it 
	 *	arrived upon. We now generate an ICMP HOST REDIRECT giving the route
	 *	we calculated.
	 */
#ifndef CONFIG_IP_NO_ICMP_REDIRECT
	if (dev == dev2 && !((iph->saddr^iph->daddr)&dev->pa_mask) && rt->rt_flags&RTF_MODIFIED)
		icmp_send(skb, ICMP_REDIRECT, ICMP_REDIR_HOST, raddr, dev);
#endif		

	/*
	 * We now allocate a new buffer, and copy the datagram into it.
	 * If the indicated interface is up and running, kick it.
	 */

	if (dev2->flags & IFF_UP)
	{
#ifdef CONFIG_IP_MASQUERADE
		/*
		 * If this fragment needs masquerading, make it so...
		 * (Dont masquerade de-masqueraded fragments)
		 */
		//NAT
		if (!(is_frag&4) && fw_res==2)
			ip_fw_masquerade(&skb, dev2);
#endif

		/*
		 *	Current design decrees we copy the packet. For identical header
		 *	lengths we could avoid it. The new skb code will let us push
		 *	data so the problem goes away then.
		 */
		//分配skb2,在ip_rcv函数中已经将skb->len赋值为iph->tot_len,所以此处分配空间要加上dev2的以太网头长度
		skb2 = alloc_skb(dev2->hard_header_len + skb->len, GFP_ATOMIC);
		/*
		 *	This is rare and since IP is tolerant of network failures
		 *	quite harmless.
		 */
		if (skb2 == NULL)
		{
			NETDEBUG(printk("\nIP: No memory available for IP forward\n"));
			return;
		}
		ptr = skb2->data;
		skb2->free = 1;
		skb2->len = skb->len + dev2->hard_header_len;//初始化skb2->len
		skb2->h.raw = ptr;

		/*
		 *	Copy the packet data into the new buffer.
		 */
		//将skb的ip包复制到skb2中，注意此处ptr加了以太网头长度
		memcpy(ptr + dev2->hard_header_len, skb->h.raw, skb->len);

		/* Now build the MAC header. */
		//构建以太网头
		(void) ip_send(skb2, raddr, skb->len, dev2, dev2->pa_addr);

		ip_statistics.IpForwDatagrams++;

		/*
		 *	See if it needs fragmenting. Note in ip_rcv we tagged
		 *	the fragment type. This must be right so that
		 *	the fragmenter does the right thing.
		 */
		//判断skb2是否需要再次分片，因为dev2的mtu可能小于dev的mtu，所以此处要进行判断
		if(skb2->len > dev2->mtu + dev2->hard_header_len)
		{
			ip_fragment(NULL,skb2,dev2, is_frag);//如果需要分片，进行分片
			kfree_skb(skb2,FREE_WRITE);
		}
		else
		{
#ifdef CONFIG_IP_ACCT		
			/*
			 *	Count mapping we shortcut
			 */
			 
			ip_fw_chk(iph,dev,ip_acct_chain,IP_FW_F_ACCEPT,1);
#endif			
			
			/*
			 *	Map service types to priority. We lie about
			 *	throughput being low priority, but it's a good
			 *	choice to help improve general usage.
			 */
			if(iph->tos & IPTOS_LOWDELAY)
				dev_queue_xmit(skb2, dev2, SOPRI_INTERACTIVE);
			else if(iph->tos & IPTOS_THROUGHPUT)
				dev_queue_xmit(skb2, dev2, SOPRI_BACKGROUND);
			else
				dev_queue_xmit(skb2, dev2, SOPRI_NORMAL);
		}
	}
}


#endif

/*
 *	This function receives all incoming IP datagrams.
 */
//ip报文接收
//1.防火墙处理
//2.选项处理
//3.原始套结字接收
//4.ip头中的协议接收
//5.ip报文转发
//注意：如果ip报文发送给本机，则执行3、4，如果不是发送给本机则进行转发
//
int ip_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
{
	struct iphdr *iph = skb->h.iph;//取出ip头
	struct sock *raw_sk=NULL;
	unsigned char hash;
	unsigned char flag = 0;
	struct inet_protocol *ipprot;
	int brd=IS_MYADDR;
	unsigned long target_addr;
	int target_strict=0;
	int is_frag=0;
#ifdef CONFIG_IP_FIREWALL
	int err;
#endif	

	ip_statistics.IpInReceives++;

	/*
	 *	Tag the ip header of this packet so we can find it
	 */

	skb->ip_hdr = iph;//将ip头指针赋给skb->ip_hdr成员

	/*
	 *	RFC1122: 3.1.2.2 MUST silently discard any IP frame that fails the checksum.
	 *	RFC1122: 3.1.2.3 MUST discard a frame with invalid source address [NEEDS FIXING].
	 *
	 *	Is the datagram acceptable?
	 *
	 *	1.	Length at least the size of an ip header
	 *	2.	Version of 4
	 *	3.	Checksums correctly. [Speed optimisation for later, skip loopback checksums]
	 *	4.	Doesn't have a bogus length
	 *	(5.	We ought to check for IP multicast addresses and undefined types.. does this matter ?)
	 */
	//判断包的合法性
	if (skb->len<sizeof(struct iphdr) || iph->ihl<5 || iph->version != 4 || ip_fast_csum((unsigned char *)iph, iph->ihl) !=0
		|| skb->len < ntohs(iph->tot_len))
	{
		ip_statistics.IpInHdrErrors++;
		kfree_skb(skb, FREE_WRITE);
		return(0);
	}

	/*
	 *	Our transport medium may have padded the buffer out. Now we know it
	 *	is IP we can trim to the true length of the frame.
	 */
	//skb->len初始化为该ip包的总长度
	skb->len=ntohs(iph->tot_len);
	
	/*
	 *	See if the firewall wants to dispose of the packet. 
	 */

#ifdef	CONFIG_IP_FIREWALL
	//防火墙
	if ((err=ip_fw_chk(iph,dev,ip_fw_blk_chain,ip_fw_blk_policy, 0))<1)
	{
		if(err==-1)
			icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0, dev);
		kfree_skb(skb, FREE_WRITE);
		return 0;	
	}

#endif
	

	/*
	 *	Next analyse the packet for options. Studies show under one packet in
	 *	a thousand have options....
	 */
	//取出目的地址(ip地址)
	target_addr = iph->daddr;

	if (iph->ihl != 5)//判断ip包是否有选项
	{ 
		//选项处理
		/* Humph.. options. Lots of annoying fiddly bits */
		
		/*
		 *	This is straight from the RFC. It might even be right ;)
		 *
		 * 	RFC 1122: 3.2.1.8 STREAMID option is obsolete and MUST be ignored.
		 *	RFC 1122: 3.2.1.8 MUST NOT crash on a zero length option.
		 *	RFC 1122: 3.2.1.8 MUST support acting as final destination of a source route.
		 */
		 
		int opt_space=4*(iph->ihl-5);
		int opt_size;
		unsigned char *opt_ptr=skb->h.raw+sizeof(struct iphdr);
		
		while(opt_space>0)
		{
			if(*opt_ptr==IPOPT_NOOP)
			{
				opt_ptr++;
				opt_space--;
				continue;
			}
			if(*opt_ptr==IPOPT_END)
				break;	/* Done */
			if(opt_space<2 || (opt_size=opt_ptr[1])<2 || opt_ptr[1]>opt_space)
			{
				/*
				 *	RFC 1122: 3.2.2.5  SHOULD send parameter problem reports.
				 */
				icmp_send(skb, ICMP_PARAMETERPROB, 0, 0, skb->dev);
				kfree_skb(skb, FREE_READ);
				return -EINVAL;
			}
			switch(opt_ptr[0])
			{
				case IPOPT_SEC:
					/* Should we drop this ?? */
					break;
				case IPOPT_SSRR:	/* These work almost the same way */
					target_strict=1;
					/* Fall through */
				case IPOPT_LSRR:
#ifdef CONFIG_IP_NOSR
					kfree_skb(skb, FREE_READ);
					return -EINVAL;
#endif					
				case IPOPT_RR:
				/*
				 *	RFC 1122: 3.2.1.8 Support for RR is OPTIONAL.
				 */
					if (iph->daddr!=skb->dev->pa_addr && (brd = ip_chk_addr(iph->daddr)) == 0) 
						break;
					if((opt_size<3) || ( opt_ptr[0]==IPOPT_RR && opt_ptr[2] > opt_size-4 ))
					{
						if(ip_chk_addr(iph->daddr))
							icmp_send(skb, ICMP_PARAMETERPROB, 0, 0, skb->dev);
						kfree_skb(skb, FREE_READ);
						return -EINVAL;
					}
					if(opt_ptr[2] > opt_size-4 )
						break;
					/* Bytes are [IPOPT_xxRR][Length][EntryPointer][Entry0][Entry1].... */
					/* This isn't going to be too portable - FIXME */
					if(opt_ptr[0]!=IPOPT_RR)
					{
						int t;
						target_addr=*(long *)(&opt_ptr[opt_ptr[2]]);	/* Get hop */
						t=ip_chk_addr(target_addr);
						if(t==IS_MULTICAST||t==IS_BROADCAST)
						{
							if(ip_chk_addr(iph->daddr))
								icmp_send(skb, ICMP_PARAMETERPROB, 0, 0, skb->dev);
							kfree_skb(skb,FREE_READ);
							return -EINVAL;						
						}
					}
					*(long *)(&opt_ptr[opt_ptr[2]])=skb->dev->pa_addr;	/* Record hop */
					break;
				case IPOPT_TIMESTAMP:
				/*
				 *	RFC 1122: 3.2.1.8 The timestamp option is OPTIONAL but if implemented
				 *	MUST meet various rules (read the spec).
				 */
					NETDEBUG(printk("ICMP: Someone finish the timestamp routine ;)\n"));
					break;
				default:
					break;
			}
			opt_ptr+=opt_size;
			opt_space-=opt_size;
		}
					
	}


	/*
	 *	Remember if the frame is fragmented.
	 */
	 
	if(iph->frag_off)//如果不为0，则进行处理
	{
		if (iph->frag_off & 0x0020)//取出mf位，查看更多分片是否为1，如果为1则is_frag的bit0位置1,标志有更多分片
			is_frag|=1;
		/*
		 *	Last fragment ?
		 */
	
		if (ntohs(iph->frag_off) & 0x1fff)//取出偏移，如果不是第一个分片is_frag的bit1位置1
			is_frag|=2;
	}
	
	/*
	 *	Do any IP forwarding required.  chk_addr() is expensive -- avoid it someday.
	 *
	 *	This is inefficient. While finding out if it is for us we could also compute
	 *	the routing table entry. This is where the great unified cache theory comes
	 *	in as and when someone implements it
	 *
	 *	For most hosts over 99% of packets match the first conditional
	 *	and don't go via ip_chk_addr. Note: brd is set to IS_MYADDR at
	 *	function entry.
	 */
	//如果ip报文时发送给本机的
	if ( iph->daddr == skb->dev->pa_addr || (brd = ip_chk_addr(iph->daddr)) != 0)
	{
#ifdef CONFIG_IP_MULTICAST	
		//多播处理，暂不分析
		if(brd==IS_MULTICAST && iph->daddr!=IGMP_ALL_HOSTS && !(dev->flags&IFF_LOOPBACK))
		{
			/*
			 *	Check it is for one of our groups
			 */
			struct ip_mc_list *ip_mc=dev->ip_mc_list;
			do
			{
				if(ip_mc==NULL)
				{	
					kfree_skb(skb, FREE_WRITE);
					return 0;
				}
				if(ip_mc->multiaddr==iph->daddr)
					break;
				ip_mc=ip_mc->next;
			}
			while(1);
		}
#endif

#ifdef CONFIG_IP_MASQUERADE
		/*
		 * Do we need to de-masquerade this fragment?
		 */
		//应该时NAT(网络地址转换)处理，路由器功能使用。有此功能，内网主机可访问路由器连接的外网
		if (ip_fw_demasquerade(skb)) 
		{
			struct iphdr *iph=skb->h.iph;
			ip_forward(skb, dev, is_frag|4, iph->daddr, 0);//ip报文转发
			kfree_skb(skb, FREE_WRITE);
			return(0);
		}
#endif

		/*
		 *	Account for the packet
		 */
 
#ifdef CONFIG_IP_ACCT
		ip_fw_chk(iph,dev,ip_acct_chain,IP_FW_F_ACCEPT,1);
#endif	

		/*
		 *	Reassemble IP fragments.
		 */
		//如果is_frag不为0，则此ip报文是分片包，进行分片重组处理
		if(is_frag)
		{
			/* Defragment. Obtain the complete packet if there is one */
			skb=ip_defrag(iph,skb,dev);//ip分片重组，如果所有分片都已接收完成，则组合成一个skb并返回，如果未接收完成，返回NULL
			if(skb==NULL)
				return 0;
			skb->dev = dev;//
			iph=skb->h.iph;
		}

		/*
		 *	Point into the IP datagram, just past the header.
		 */

		skb->ip_hdr = iph;//skb->ip_hdr赋值
		skb->h.raw += iph->ihl*4;//skb->h.raw指向ip数据部分

		/*
		 *	Deliver to raw sockets. This is fun as to avoid copies we want to make no surplus copies.
		 *
		 *	RFC 1122: SHOULD pass TOS value up to the transport layer.
		 */
		//计算原始套结字在raw_prot.sock_array哈希链表中对应的hash值 
		hash = iph->protocol & (SOCK_ARRAY_SIZE-1);

		/* 
		 *	If there maybe a raw socket we must check - if not we don't care less 
		 */
		//原始套结字接收，暂不分析
		if((raw_sk=raw_prot.sock_array[hash])!=NULL)
		{
			struct sock *sknext=NULL;
			struct sk_buff *skb1;
			raw_sk=get_sock_raw(raw_sk, hash,  iph->saddr, iph->daddr);
			if(raw_sk)	/* Any raw sockets */
			{
				do
				{
					/* Find the next */
					sknext=get_sock_raw(raw_sk->next, hash, iph->saddr, iph->daddr);
					if(sknext)
						skb1=skb_clone(skb, GFP_ATOMIC);
					else
						break;	/* One pending raw socket left */
					if(skb1)
						raw_rcv(raw_sk, skb1, dev, iph->saddr,iph->daddr);
					raw_sk=sknext;
				}
				while(raw_sk!=NULL);
				
				/*
				 *	Here either raw_sk is the last raw socket, or NULL if none 
				 */
				 
				/*
				 *	We deliver to the last raw socket AFTER the protocol checks as it avoids a surplus copy 
				 */
			}
		}
	
		/*
		 *	skb->h.raw now points at the protocol beyond the IP header.
		 */
		//相关协议接收：TCP，UDP，ICMP，先计算inet_protos哈希表中对应的hash值
		hash = iph->protocol & (MAX_INET_PROTOS -1);
		//遍历链表，查找所属
		for (ipprot = (struct inet_protocol *)inet_protos[hash];ipprot != NULL;ipprot=(struct inet_protocol *)ipprot->next)
		{
			struct sk_buff *skb2;
	
			if (ipprot->protocol != iph->protocol)
				continue;
		       /*
			* 	See if we need to make a copy of it.  This will
			* 	only be set if more than one protocol wants it.
			* 	and then not for the last one. If there is a pending
			*	raw delivery wait for that
			*/
			//如果ipprot->copy是1，则ipprot对应的传输层协议接收函数内部会释放此skb，
			if (ipprot->copy || raw_sk)
			{
				skb2 = skb_clone(skb, GFP_ATOMIC);//复制一个和skb一样的，赋值给skb2
				if(skb2==NULL)
					continue;
			}
			else
			{
				skb2 = skb;
			}
			flag = 1;

		       /*
			*	Pass on the datagram to each protocol that wants it,
			*	based on the datagram protocol.  We should really
			*	check the protocol handler's return values here...
			*/

			ipprot->handler(skb2, dev, NULL, iph->daddr,
				(ntohs(iph->tot_len) - (iph->ihl * 4)),
				iph->saddr, 0, ipprot);

		}

		/*
		 *	All protocols checked.
		 *	If this packet was a broadcast, we may *not* reply to it, since that
		 *	causes (proven, grin) ARP storms and a leakage of memory (i.e. all
		 *	ICMP reply messages get queued up for transmission...)
		 */
		//raw_sk不为NULL，则进行原始套结字接收
		if(raw_sk!=NULL)	/* Shift to last raw user */
			raw_rcv(raw_sk, skb, dev, iph->saddr, iph->daddr);
		else if (!flag)		/* Free and report errors */
		{
			if (brd != IS_BROADCAST && brd!=IS_MULTICAST)
				icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, 0, dev);	
			kfree_skb(skb, FREE_WRITE);//如果协议为接收，则释放skb
		}

		return(0);//发往本机的ip报文处理完成，返回
	}

	//以下是非本机报文处理
	/*
	 *	Do any IP forwarding required.  chk_addr() is expensive -- avoid it someday.
	 *
	 *	This is inefficient. While finding out if it is for us we could also compute
	 *	the routing table entry. This is where the great unified cache theory comes
	 *	in as and when someone implements it
	 *
	 *	For most hosts over 99% of packets match the first conditional
	 *	and don't go via ip_chk_addr. Note: brd is set to IS_MYADDR at
	 *	function entry.
	 */
	
	/*
	 *	Don't forward multicast or broadcast frames.
	 */
	//如果包不是本机的包或brd等于IS_BROADCAST，释放skb，返回
	if(skb->pkt_type!=PACKET_HOST || brd==IS_BROADCAST)
	{
		kfree_skb(skb,FREE_WRITE);
		return 0;
	}

	/*
	 *	The packet is for another target. Forward the frame
	 */
	//ip报文转发
#ifdef CONFIG_IP_FORWARD
	ip_forward(skb, dev, is_frag, target_addr, target_strict);
#else
/*	printk("Machine %lx tried to use us as a forwarder to %lx but we have forwarding disabled!\n",
			iph->saddr,iph->daddr);*/
	ip_statistics.IpInAddrErrors++;
#endif
	/*
	 *	The forwarder is inefficient and copies the packet. We
	 *	free the original now.
	 */
	//释放skb
	kfree_skb(skb, FREE_WRITE);
	return(0);
}
	

/*
 *	Loop a packet back to the sender.
 */
//skb数据通过lo设备发送数据
//当数据包是广播数据包是，会调用到此函数，将广播的数据包发送给lo设备
//@old_dev:旧的网卡设备
//@skb:要发送的数据
static void ip_loopback(struct device *old_dev, struct sk_buff *skb)
{
	extern struct device loopback_dev;
	struct device *dev=&loopback_dev;//将lo网卡设备赋值给dev
	int len=skb->len-old_dev->hard_header_len;//计算lo设备下此包的数据部分长度，ip头长度算在内
	struct sk_buff *newskb=alloc_skb(len+dev->hard_header_len, GFP_ATOMIC);//分配一个新skb，数据长度加以链路层头长度
	
	if(newskb==NULL)
		return;
	//初始化新分配的skb	
	newskb->link3=NULL;
	newskb->sk=NULL;
	newskb->dev=dev;
	newskb->saddr=skb->saddr;
	newskb->daddr=skb->daddr;
	newskb->raddr=skb->raddr;
	newskb->free=1;
	newskb->lock=0;
	newskb->users=0;
	newskb->pkt_type=skb->pkt_type;
	newskb->len=len+dev->hard_header_len;
	
	//此处要构建链路层头，调用ip_send函数	
	newskb->ip_hdr=(struct iphdr *)(newskb->data+ip_send(newskb, skb->ip_hdr->daddr, len, dev, skb->ip_hdr->saddr));
	memcpy(newskb->ip_hdr,skb->ip_hdr,len);//赋值数据部分，把ip头部及ip数据部分全部赋值给新的skb

	/* Recurse. The device check against IFF_LOOPBACK will stop infinite recursion */
		
	/*printk("Loopback output queued [%lX to %lX].\n", newskb->ip_hdr->saddr,newskb->ip_hdr->daddr);*/
	ip_queue_xmit(NULL, dev, newskb, 1);//提交skb,free参数传入1，
}


/*
 * Queues a packet to be sent, and starts the transmitter
 * if necessary.  if free = 1 then we free the block after
 * transmit, otherwise we don't. If free==2 we not only
 * free the block but also don't assign a new ip seq number.
 * This routine also needs to put in the total length,
 * and compute the checksum
 */
//报文提交函数
//iq_queue_xmit和ip_build_xmit
void ip_queue_xmit(struct sock *sk, struct device *dev,
	      struct sk_buff *skb, int free)
{
	struct iphdr *iph;
	unsigned char *ptr;

	/* Sanity check */
	if (dev == NULL)
	{
		NETDEBUG(printk("IP: ip_queue_xmit dev = NULL\n"));
		return;
	}

	IS_SKB(skb);

	/*
	 *	Do some book-keeping in the packet for later
	 */


	skb->dev = dev;
	skb->when = jiffies;

	/*
	 *	Find the IP header and set the length. This is bad
	 *	but once we get the skb data handling code in the
	 *	hardware will push its header sensibly and we will
	 *	set skb->ip_hdr to avoid this mess and the fixed
	 *	header length problem
	 */
	//找到iph，然后iph->tot_len赋值
	ptr = skb->data;
	ptr += dev->hard_header_len;
	iph = (struct iphdr *)ptr;
	skb->ip_hdr = iph;  //skb->ip_hdr赋值
	iph->tot_len = ntohs(skb->len-dev->hard_header_len);//iph->tot_len赋值
	
	//防火墙检测
#ifdef CONFIG_IP_FIREWALL
	if(ip_fw_chk(iph, dev, ip_fw_blk_chain, ip_fw_blk_policy, 0) != 1)
		/* just don't send this packet */
		return;
#endif	

	/*
	 *	No reassigning numbers to fragments...
	 */
	//free参数只有ip_fragment函数传入为2，因为ip_fragment函数是ip_queue_xmit函数调用的，所以已经给iph的id赋过值
	//其他地方调用时传入的值为1，此处就要对iph->id进行赋值
	if(free!=2)
		iph->id      = htons(ip_id_count++);
	else
		free=1;//如果free传入时为2，在此将其改为1

	/* All buffers without an owner socket get freed */
	if (sk == NULL)//入参sk如果为NULL，则free赋值为1
		free = 1;

	skb->free = free;//将free赋值给skb->free

	/*
	 *	Do we need to fragment. Again this is inefficient.
	 *	We need to somehow lock the original buffer and use
	 *	bits of it.
	 */
	//如果skb->len大于mtu+链路层头长度，则进行分片发送处理
	if(skb->len > dev->mtu + dev->hard_header_len)
	{
		ip_fragment(sk,skb,dev,0);//分片发送处理
		IS_SKB(skb);
		kfree_skb(skb,FREE_WRITE);//释放skb
		return;
	}

	/*
	 *	Add an IP checksum
	 */
	//如果不需要分片处理，以下代码开始发送，先计算ip头校验和
	ip_send_check(iph);

	/*
	 *	Print the frame when debugging
	 */

	/*
	 *	More debugging. You cannot queue a packet already on a list
	 *	Spot this and moan loudly.
	 */
	//如果skb存在于链表中，将其摘除
	if (skb->next != NULL)
	{
		NETDEBUG(printk("ip_queue_xmit: next != NULL\n"));
		skb_unlink(skb);//从链表中摘除
	}

	/*
	 *	If a sender wishes the packet to remain unfreed
	 *	we add it to his send queue. This arguably belongs
	 *	in the TCP level since nobody else uses it. BUT
	 *	remember IPng might change all the rules.
	 */

	if (!free)
	{
		unsigned long flags;
		/* The socket now has more outstanding blocks */

		sk->packets_out++;

		/* Protect the list for a moment */
		save_flags(flags);
		cli();

		if (skb->link3 != NULL)
		{
			NETDEBUG(printk("ip.c: link3 != NULL\n"));
			skb->link3 = NULL;
		}
		if (sk->send_head == NULL)
		{
			sk->send_tail = skb;
			sk->send_head = skb;
		}
		else
		{
			sk->send_tail->link3 = skb;
			sk->send_tail = skb;
		}
		/* skb->link3 is NULL */

		/* Interrupt restore */
		restore_flags(flags);
	}
	else
		/* Remember who owns the buffer */
		skb->sk = sk;//记录skb属于哪个sk

	/*
	 *	If the indicated interface is up and running, send the packet.
	 */
	 
	ip_statistics.IpOutRequests++;
#ifdef CONFIG_IP_ACCT
	ip_fw_chk(iph,dev,ip_acct_chain,IP_FW_F_ACCEPT,1);
#endif	
	
#ifdef CONFIG_IP_MULTICAST	
	//多播处理，暂不分析
	/*
	 *	Multicasts are looped back for other local users
	 */
	 
	if (MULTICAST(iph->daddr) && !(dev->flags&IFF_LOOPBACK))
	{
		if(sk==NULL || sk->ip_mc_loop)
		{
			if(iph->daddr==IGMP_ALL_HOSTS)
				ip_loopback(dev,skb);
			else
			{
				struct ip_mc_list *imc=dev->ip_mc_list;
				while(imc!=NULL)
				{
					if(imc->multiaddr==iph->daddr)
					{
						ip_loopback(dev,skb);
						break;
					}
					imc=imc->next;
				}
			}
		}
		/* Multicasts with ttl 0 must not go beyond the host */
		
		if(skb->ip_hdr->ttl==0)
		{
			kfree_skb(skb, FREE_READ);
			return;
		}
	}
#endif
	//如果网卡设备支持广播，且目的地址是广播地址，且网卡设备不是lo，则将skb发送到lo设备
	if((dev->flags&IFF_BROADCAST) && iph->daddr==dev->pa_brdaddr && !(dev->flags&IFF_LOOPBACK))
		ip_loopback(dev,skb);
	//如果网卡设备启动	
	if (dev->flags & IFF_UP)
	{
		/*
		 *	If we have an owner use its priority setting,
		 *	otherwise use NORMAL
		 */
		//将skb提交给dev
		//
		if (sk != NULL)
		{
			dev_queue_xmit(skb, dev, sk->priority);
		}
		else
		{
			dev_queue_xmit(skb, dev, SOPRI_NORMAL);
		}
	}
	else//网卡设备未启动，释放skb
	{
		ip_statistics.IpOutDiscards++;
		if (free)
			kfree_skb(skb, FREE_WRITE);
	}
}



#ifdef CONFIG_IP_MULTICAST

/*
 *	Write an multicast group list table for the IGMP daemon to
 *	read.
 */
 
int ip_mc_procinfo(char *buffer, char **start, off_t offset, int length)
{
	off_t pos=0, begin=0;
	struct ip_mc_list *im;
	unsigned long flags;
	int len=0;
	struct device *dev;

	len=sprintf(buffer,"Device    : Count\tGroup    Users Timer\n");  
	save_flags(flags);
	cli();

	for(dev = dev_base; dev; dev = dev->next)
	{
		if((dev->flags&IFF_UP)&&(dev->flags&IFF_MULTICAST))
		{
			len+=sprintf(buffer+len,"%-10s: %5d\n",
					dev->name, dev->mc_count);
			for(im = dev->ip_mc_list; im; im = im->next)
			{
				len+=sprintf(buffer+len,
						"\t\t\t%08lX %5d %d:%08lX\n",
						im->multiaddr, im->users,
						im->tm_running, im->timer.expires);
				pos=begin+len;
				if(pos<offset)
				{
					len=0;
					begin=pos;
				}
				if(pos>offset+length)
					break;
			}
		}
	}
	restore_flags(flags);
	*start=buffer+(offset-begin);
	len-=(offset-begin);
	if(len>length)
		len=length;	
	return len;
}


#endif	
/*
 *	Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
 *	an IP socket.
 *
 *	We implement IP_TOS (type of service), IP_TTL (time to live).
 *
 *	Next release we will sort out IP_OPTIONS since for some people are kind of important.
 */

static struct device *ip_mc_find_devfor(unsigned long addr)
{
	struct device *dev;
	for(dev = dev_base; dev; dev = dev->next)
	{
		if((dev->flags&IFF_UP)&&(dev->flags&IFF_MULTICAST)&&
			(dev->pa_addr==addr))
			return dev;
	}

	return NULL;
}
//ip选项设置
int ip_setsockopt(struct sock *sk, int level, int optname, char *optval, int optlen)
{
	int val,err;
	unsigned char ucval;
#if defined(CONFIG_IP_FIREWALL) || defined(CONFIG_IP_ACCT)
	struct ip_fw tmp_fw;
#endif	
	if (optval == NULL)
		return(-EINVAL);

	err=verify_area(VERIFY_READ, optval, sizeof(int));
	if(err)
		return err;

	val = get_fs_long((unsigned long *)optval);
	ucval=get_fs_byte((unsigned char *)optval);

	if(level!=SOL_IP)
		return -EOPNOTSUPP;

	switch(optname)
	{
		case IP_TOS:
			if(val<0||val>255)
				return -EINVAL;
			sk->ip_tos=val;
			if(val==IPTOS_LOWDELAY)
				sk->priority=SOPRI_INTERACTIVE;
			if(val==IPTOS_THROUGHPUT)
				sk->priority=SOPRI_BACKGROUND;
			return 0;
		case IP_TTL:
			if(val<1||val>255)
				return -EINVAL;
			sk->ip_ttl=val;
			return 0;
#ifdef CONFIG_IP_MULTICAST
		case IP_MULTICAST_TTL: 
		{
			sk->ip_mc_ttl=(int)ucval;
	                return 0;
		}
		case IP_MULTICAST_LOOP: 
		{
			if(ucval!=0 && ucval!=1)
				 return -EINVAL;
			sk->ip_mc_loop=(int)ucval;
			return 0;
		}
		case IP_MULTICAST_IF: 
		{
			struct in_addr addr;
			struct device *dev=NULL;
			
			/*
			 *	Check the arguments are allowable
			 */

			err=verify_area(VERIFY_READ, optval, sizeof(addr));
			if(err)
				return err;
				
			memcpy_fromfs(&addr,optval,sizeof(addr));
			
			
			/*
			 *	What address has been requested
			 */
			
			if(addr.s_addr==INADDR_ANY)	/* Default */
			{
				sk->ip_mc_name[0]=0;
				return 0;
			}
			
			/*
			 *	Find the device
			 */
			 
			dev=ip_mc_find_devfor(addr.s_addr);
						
			/*
			 *	Did we find one
			 */
			 
			if(dev) 
			{
				strcpy(sk->ip_mc_name,dev->name);
				return 0;
			}
			return -EADDRNOTAVAIL;
		}
		
		case IP_ADD_MEMBERSHIP: 
		{
		
/*
 *	FIXME: Add/Del membership should have a semaphore protecting them from re-entry
 */
			struct ip_mreq mreq;
			unsigned long route_src;
			struct rtable *rt;
			struct device *dev=NULL;
			
			/*
			 *	Check the arguments.
			 */

			err=verify_area(VERIFY_READ, optval, sizeof(mreq));
			if(err)
				return err;

			memcpy_fromfs(&mreq,optval,sizeof(mreq));

			/* 
			 *	Get device for use later
			 */

			if(mreq.imr_interface.s_addr==INADDR_ANY) 
			{
				/*
				 *	Not set so scan.
				 */
				if((rt=ip_rt_route(mreq.imr_multiaddr.s_addr,NULL, &route_src))!=NULL)
				{
					dev=rt->rt_dev;
					rt->rt_use--;
				}
			}
			else
			{
				/*
				 *	Find a suitable device.
				 */
				
				dev=ip_mc_find_devfor(mreq.imr_interface.s_addr);
			}
			
			/*
			 *	No device, no cookies.
			 */
			 
			if(!dev)
				return -ENODEV;
				
			/*
			 *	Join group.
			 */
			 
			return ip_mc_join_group(sk,dev,mreq.imr_multiaddr.s_addr);
		}
		
		case IP_DROP_MEMBERSHIP: 
		{
			struct ip_mreq mreq;
			struct rtable *rt;
                        unsigned long route_src;
			struct device *dev=NULL;

			/*
			 *	Check the arguments
			 */
			 
			err=verify_area(VERIFY_READ, optval, sizeof(mreq));
			if(err)
				return err;

			memcpy_fromfs(&mreq,optval,sizeof(mreq));

			/*
			 *	Get device for use later 
			 */
 
			if(mreq.imr_interface.s_addr==INADDR_ANY) 
			{
				if((rt=ip_rt_route(mreq.imr_multiaddr.s_addr,NULL, &route_src))!=NULL)
			        {
					dev=rt->rt_dev;
					rt->rt_use--;
				}
			}
			else 
			{
			
				dev=ip_mc_find_devfor(mreq.imr_interface.s_addr);
			}
			
			/*
			 *	Did we find a suitable device.
			 */
			 
			if(!dev)
				return -ENODEV;
				
			/*
			 *	Leave group
			 */
			 
			return ip_mc_leave_group(sk,dev,mreq.imr_multiaddr.s_addr);
		}
#endif			
#ifdef CONFIG_IP_FIREWALL
		case IP_FW_ADD_BLK:
		case IP_FW_DEL_BLK:
		case IP_FW_ADD_FWD:
		case IP_FW_DEL_FWD:
		case IP_FW_CHK_BLK:
		case IP_FW_CHK_FWD:
		case IP_FW_FLUSH_BLK:
		case IP_FW_FLUSH_FWD:
		case IP_FW_ZERO_BLK:
		case IP_FW_ZERO_FWD:
		case IP_FW_POLICY_BLK:
		case IP_FW_POLICY_FWD:
			if(!suser())
				return -EPERM;
			if(optlen>sizeof(tmp_fw) || optlen<1)
				return -EINVAL;
			err=verify_area(VERIFY_READ,optval,optlen);
			if(err)
				return err;
			memcpy_fromfs(&tmp_fw,optval,optlen);
			err=ip_fw_ctl(optname, &tmp_fw,optlen);
			return -err;	/* -0 is 0 after all */
			
#endif
#ifdef CONFIG_IP_ACCT
		case IP_ACCT_DEL:
		case IP_ACCT_ADD:
		case IP_ACCT_FLUSH:
		case IP_ACCT_ZERO:
			if(!suser())
				return -EPERM;
			if(optlen>sizeof(tmp_fw) || optlen<1)
				return -EINVAL;
			err=verify_area(VERIFY_READ,optval,optlen);
			if(err)
				return err;
			memcpy_fromfs(&tmp_fw, optval,optlen);
			err=ip_acct_ctl(optname, &tmp_fw,optlen);
			return -err;	/* -0 is 0 after all */
#endif
		/* IP_OPTIONS and friends go here eventually */
		default:
			return(-ENOPROTOOPT);
	}
}

/*
 *	Get the options. Note for future reference. The GET of IP options gets the
 *	_received_ ones. The set sets the _sent_ ones.
 */
//ip选项获取
int ip_getsockopt(struct sock *sk, int level, int optname, char *optval, int *optlen)
{
	int val,err;
#ifdef CONFIG_IP_MULTICAST
	int len;
#endif
	
	if(level!=SOL_IP)
		return -EOPNOTSUPP;

	switch(optname)
	{
		case IP_TOS:
			val=sk->ip_tos;
			break;
		case IP_TTL:
			val=sk->ip_ttl;
			break;
#ifdef CONFIG_IP_MULTICAST			
		case IP_MULTICAST_TTL:
			val=sk->ip_mc_ttl;
			break;
		case IP_MULTICAST_LOOP:
			val=sk->ip_mc_loop;
			break;
		case IP_MULTICAST_IF:
			err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
			if(err)
  				return err;
  			len=strlen(sk->ip_mc_name);
  			err=verify_area(VERIFY_WRITE, optval, len);
		  	if(err)
  				return err;
  			put_fs_long(len,(unsigned long *) optlen);
			memcpy_tofs((void *)optval,sk->ip_mc_name, len);
			return 0;
#endif
		default:
			return(-ENOPROTOOPT);
	}
	err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
	if(err)
		return err;
	put_fs_long(sizeof(int),(unsigned long *) optlen);

	err=verify_area(VERIFY_WRITE, optval, sizeof(int));
	if(err)
		return err;
	put_fs_long(val,(unsigned long *)optval);

	return(0);
}

/*
 *	Build and send a packet, with as little as one copy
 *
 *	Doesn't care much about ip options... option length can be
 *	different for fragment at 0 and other fragments.
 *
 *	Note that the fragment at the highest offset is sent first,
 *	so the getfrag routine can fill in the TCP/UDP checksum header
 *	field in the last fragment it sends... actually it also helps
 * 	the reassemblers, they can put most packets in at the head of
 *	the fragment queue, and they know the total size in advance. This
 *	last feature will measurable improve the Linux fragment handler.
 *
 *	The callback has five args, an arbitrary pointer (copy of frag),
 *	the source IP address (may depend on the routing table), the 
 *	destination adddress (char *), the offset to copy from, and the
 *	length to be copied.
 * 
 */
//@sk:sock结构体
//@getfrag:函数指针，回调函数，udp,raw协议在发送数据时调用ip_build_xmit函数传递此回调函数，用于构建其协议头和填充用户层数据
//@frag:传递给getfrag回调函数的第一参数
//@length:ip包数据长度(带有传输层(udp,)头长度)
//@daddr:目的ip地址
//@flags:标志
//@type:协议类型，UDP为IPPROTO_UDP,raw为IPPROTO_RAW
int ip_build_xmit(struct sock *sk,
		   void getfrag (void *,
				 int,
				 char *,
				 unsigned int,
				 unsigned int),
		   void *frag,
		   unsigned short int length,
		   int daddr,
		   int flags,
		   int type) 
{
	struct rtable *rt;
	unsigned int fraglen, maxfraglen, fragheaderlen;
	int offset, mf;
	unsigned long saddr;
	unsigned short id;
	struct iphdr *iph;
	int local=0;
	struct device *dev;

	//多播，暂不分析
#ifdef CONFIG_INET_MULTICAST	
	if(sk && MULTICAST(daddr) && *sk->ip_mc_name)
	{
		dev=dev_get(skb->ip_mc_name);
		if(!dev)
			return -ENODEV;
		rt=NULL;
	}
	else
	{
#endif	
		/*
		 *	Perform the IP routing decisions
		 */
	 	//如果为本地路由，或flags设置MSG_DONTROUTE，则local置1
		if(sk->localroute || flags&MSG_DONTROUTE)
			local=1;
		//sk->ip_route_cache缓存路由信息，可以快速找到网卡设备，第一次是没有缓存的，需要从路由表查找
		rt = sk->ip_route_cache;
		
		/*
		 *	See if the routing cache is outdated. We need to clean this up once we are happy it is reliable
		 *	by doing the invalidation actively in the route change and header change.
		 */
		/*
		 * sk中不仅仅缓存了路由表，而且还要缓存整个以太网头信息（14个字节，存放在sk->ip_hcache_data中），当第一次发送数据
		 * 时，路由表和以太网头信息都未缓存，首先缓存路由表信息，之后发送时，再进行以太网头信息缓存,
		 */
		saddr=sk->ip_route_saddr;
		//检查路由缓存的有效性，如果第一次发送数据，此时未缓存路由表信息，将进行路由查找，并进行缓存
		if(!rt || sk->ip_route_stamp != rt_stamp || daddr!=sk->ip_route_daddr || sk->ip_route_local!=local || sk->saddr!=sk->ip_route_saddr)
		{
			//路由缓存无效或还没有缓存，那进行路由查找
			if(local)
				rt = ip_rt_local(daddr, NULL, &saddr);
			else
				rt = ip_rt_route(daddr, NULL, &saddr);
			sk->ip_route_local=local;
			sk->ip_route_daddr=daddr;
			sk->ip_route_saddr=saddr;
			sk->ip_route_stamp=rt_stamp;//rt_stamp定义在route.c中，当路由表有变动时，rt_stamp会加1,sk中记录此值，当sk中的记录和rt_stamp不一样时，就要重新查找路由表
			sk->ip_route_cache=rt;//缓存路由表信息
			sk->ip_hcache_ver=NULL;//此成员是个指针，会指向arp_cache_stamp,等缓存以太网头信息时，设置此值
			sk->ip_hcache_state= 0;//第一次发送，将sk->ip_hcache_state置为0，当缓存失败也设置为0(查看ethernet/eth.c eth_header_cache函数调用arp_find_cache函数返回失败，sk->ip_hcache_state置为0)，下面会进行判断
		}
		else if(rt)//缓存路由表有效
		{
			/*
			 *	Attempt header caches only if the cached route is being reused. Header cache
			 *	is not ultra cheap to set up. This means we only set it up on the second packet,
			 *	so one shot communications are not slowed. We assume (seems reasonable) that 2 is
			 *	probably going to be a stream of data.
			 */
			//查看是否需要缓存以太网头，根据sk->ip_hcache_ver和sk->ip_hcache_state或arp表是否更新过来判断
			//sk->ip_hcache_state有3个值 0：第一次发送数据或缓存失败  1：缓存成功  -1：不能缓存
			//值为0进入，此时sk->ip_hcache_ver一定==NULL，进行缓存；值为1进入，sk->ip_hcache_ver一定!=NULL，arp表是否更新，如果更新
			//则进行缓存；否则将sk->ip_hcache_state设置为-1，标志以后都不能缓存了，为什么不能缓存了？
			if(rt->rt_dev->header_cache && sk->ip_hcache_state!= -1)
			{
				//当sk->ip_hcache_ver==NULL则表明以太网头还未缓存，当sk->ip_hcache_stamp!=*sk->ip_hcache_ver则表明arp表更新过
				//ethernet/eth.c中eth_header_cache函数中对sk->ip_hcache_ver和sk->ip_hcache_stamp进行赋值，并有说明
				//如果sk->ip_hcache_ver!=NULL && sk->ip_hcache_stamp==*sk->ip_hcache_ver则执行else，此后都不进行缓存了
				if(sk->ip_hcache_ver==NULL || sk->ip_hcache_stamp!=*sk->ip_hcache_ver)
					rt->rt_dev->header_cache(rt->rt_dev,sk,saddr,daddr);//缓存以太网头，缓存成功，sk->ip_hcache_state=1，缓存失败，sk->ip_hcache_state=0
				else//不能缓存，sk->ip_hcache_state=-1,下次判断if(rt->rt_dev->header_cache && sk->ip_hcache_state!= -1)就不回进入了
					//为什么不能缓存了？心态崩了！！！
					/* Can't cache. Remember this */
					sk->ip_hcache_state= -1;
			}
		}
		//路由表未找到，返回错误码
		if (rt == NULL) 
		{
	 		ip_statistics.IpOutNoRoutes++;
			return(-ENETUNREACH);
		}
	
		if (sk->saddr && (!LOOPBACK(sk->saddr) || LOOPBACK(daddr)))
			saddr = sk->saddr;
		//网卡设备	
		dev=rt->rt_dev;
#ifdef CONFIG_INET_MULTICAST
	}
#endif		

	/*
	 *	Now compute the buffer space we require
	 */ 

	fragheaderlen = dev->hard_header_len;//fragheaderlen先赋值为以太网头长度(14字节长度)
	if(type != IPPROTO_RAW)//当type==IPPROTO_RAW时，原始套结字对ip头部进行构建
		fragheaderlen += 20; //因为此版本不支持ip选项，所以20表示的是ip未加选项长度的20字节头长度
		
	/*
	 *	Fragheaderlen is the size of 'overhead' on each buffer. Now work
	 *	out the size of the frames to send.
	 */
	//计算一片最大长度，如果是以太网，mtu为1500,减去ip20字节长度，~7操作是为了保证是8的整数倍,然后加上以太网头和ip头长度1520
	maxfraglen = ((dev->mtu-20) & ~7) + fragheaderlen;
	
	/*
	 *	Start at the end of the frame by handling the remainder.
	 */
	//发送从最后一片开始，此处获取最后一片的偏移
	offset = length - (length % (maxfraglen - fragheaderlen));
	
	/*
	 *	Amount of memory to allocate for final fragment.
	 */
	 
	fraglen = length - offset + fragheaderlen; //最后一片要分配的内存大小
	
	if(fraglen==0) //此处有bug，应该为if((length - offset) == 0)  此处判断是为了知道发送的数据是不是正好为1480的整数倍，如果是，就得向前移动一个片
	{
		fraglen = maxfraglen; //fraglen为maxfraglen(1480字节长度)
		offset -= maxfraglen-fragheaderlen;//offset向前移动一片
	}
	
	
	/*
	 *	The last fragment will not have MF (more fragments) set.
	 */
	 
	mf = 0; //more fragments标志 因为为最后一片，所以设置为0

	/*
	 *	Can't fragment raw packets 
	 */
	//如果是原始套结字，不允许分片
	if (type == IPPROTO_RAW && offset > 0)
 		return(-EMSGSIZE);

	/*
	 *	Get an identifier
	 */
	 
	id = htons(ip_id_count++); //标示符

	/*
	 *	Being outputting the bytes.
	 */
	//以下进行分片发送处理 
	do 
	{
		struct sk_buff * skb;
		int error;
		char *data;

		/*
		 *	Get the memory we require.
		 */
        //为skb分配空间，如果是第一次进入到这儿，fraglen大小就是上面fraglen = length - offset + fragheaderlen;计算出来的大小，或为maxfraglen
		skb = sock_alloc_send_skb(sk, fraglen, 0, &error);
		if (skb == NULL)
			return(error);

		/*
		 *	Fill in the control structures
		 */
		//进行初始化新分配的skb 
		skb->next = skb->prev = NULL;
		skb->dev = dev;
		skb->when = jiffies;
		skb->free = 1; /* dubious, this one */
		skb->sk = sk;
		skb->arp = 0;
		skb->saddr = saddr;
		skb->raddr = (rt&&rt->rt_gateway) ? rt->rt_gateway : daddr;
		skb->len = fraglen;

		/*
		 *	Save us ARP and stuff. In the optimal case we do no route lookup (route cache ok)
		 *	no ARP lookup (arp cache ok) and output. The cache checks are still too slow but
		 *	this can be fixed later. For gateway routes we ought to have a rt->.. header cache
		 *	pointer to speed header cache builds for identical targets.
		 */
		//1、构建链路层头
		if(sk->ip_hcache_state>0) //如果sk中缓存了链路层头
		{
			memcpy(skb->data,sk->ip_hcache_data, dev->hard_header_len);
			skb->arp=1;
		}
		else if (dev->hard_header)
		{
			if(dev->hard_header(skb->data, dev, ETH_P_IP, NULL, NULL, 0,  NULL)>0) //当dev是回环网卡时，就会构建成功，skb->arp置1，调用dev_queue_xmit时，不再调用eth_rebuild_header函数
				skb->arp=1;
		}
		
		/*
		 *	Find where to start putting bytes.
		 */
		 
		data = (char *)skb->data + dev->hard_header_len;
		iph = (struct iphdr *)data;

		/*
		 *	Only write IP header onto non-raw packets 
		 */
		//原始套结字不在这里构建ip头部，原始套结字自己构建 
		if(type != IPPROTO_RAW) 
		{
			//2、构建ip头
			iph->version = 4;
			iph->ihl = 5; /* ugh */
			iph->tos = sk->ip_tos;
			iph->tot_len = htons(fraglen - fragheaderlen + iph->ihl*4);
			iph->id = id;
			iph->frag_off = htons(offset>>3);
			iph->frag_off |= mf;
#ifdef CONFIG_IP_MULTICAST
			if (MULTICAST(daddr))
				iph->ttl = sk->ip_mc_ttl;
			else
#endif
				iph->ttl = sk->ip_ttl;
			iph->protocol = type;
			iph->check = 0;
			iph->saddr = saddr;
			iph->daddr = daddr;
			iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
			data += iph->ihl*4;
			
			/*
			 *	Any further fragments will have MF set.
			 */
			 
			mf = htons(IP_MF);
		}
		
		/*
		 *	User data callback
		 */
		//此函数中可能会构建传输层头,offset为0时，构建传输层头部，原始套结字时，ip头也会构建
		//frag参数调用者传入的，saddr:源ip地址  data:ip报文数据部分，如果是原始套结字，那就是链路层数据部分
		//offset:偏移   fraglen-fragheaderlen:要复制的数据长度
		getfrag(frag, saddr, data, offset, fraglen-fragheaderlen);
		
		/*
		 *	Account for the fragment.
		 */
		 
#ifdef CONFIG_IP_ACCT
		if(!offset)
			ip_fw_chk(iph, dev, ip_acct_chain, IP_FW_F_ACCEPT, 1);
#endif	
		offset -= (maxfraglen-fragheaderlen);
		fraglen = maxfraglen;

#ifdef CONFIG_IP_MULTICAST

		/*
		 *	Multicasts are looped back for other local users
		 */
	 
		if (MULTICAST(daddr) && !(dev->flags&IFF_LOOPBACK)) 
		{
			/*
			 *	Loop back any frames. The check for IGMP_ALL_HOSTS is because
			 *	you are always magically a member of this group.
			 */
			 
			if(sk==NULL || sk->ip_mc_loop) 
			{
				if(skb->daddr==IGMP_ALL_HOSTS)
					ip_loopback(rt->rt_dev,skb);
				else 
				{
					struct ip_mc_list *imc=rt->rt_dev->ip_mc_list;
					while(imc!=NULL) 
					{
						if(imc->multiaddr==daddr) 
						{
							ip_loopback(rt->rt_dev,skb);
							break;
						}
						imc=imc->next;
					}
				}
			}

			/*
			 *	Multicasts with ttl 0 must not go beyond the host. Fixme: avoid the
			 *	extra clone.
			 */

			if(skb->ip_hdr->ttl==0)
				kfree_skb(skb, FREE_READ);
		}
#endif
		/*
		 *	Now queue the bytes into the device.
		 */
		 
		if (dev->flags & IFF_UP) 
		{
			dev_queue_xmit(skb, dev, sk->priority);
		} 
		else 
		{
			/*
			 *	Whoops... 
			 *
			 *	FIXME:	There is a small nasty here. During the ip_build_xmit we could
			 *	page fault between the route lookup and device send, the device might be
			 *	removed and unloaded.... We need to add device locks on this.
			 */
			 
			ip_statistics.IpOutDiscards++;
			kfree_skb(skb, FREE_WRITE);
			return(0); /* lose rest of fragments */
		}
	} 
	while (offset >= 0);
	
	return(0);
}
    

/*
 *	IP protocol layer initialiser
 */
//定义链路层接收结构体，ip_init函数中将此变量注册到链路层，当链路层收到数据包时，判断数据包
//是否是ip报文，如果是则会调用ip_packet_type.func，即ip_rcv函数
static struct packet_type ip_packet_type =
{
	0,	/* MUTTER ntohs(ETH_P_IP),*/
	NULL,	/* All devices */
	ip_rcv,
	NULL,
	NULL,
};

/*
 *	Device notifier
 */
//网卡设备改变回调函数，当网卡设备状态发送变化时(启动或关闭),调用此回调函数 
static int ip_rt_event(unsigned long event, void *ptr)
{
	if(event==NETDEV_DOWN)//事件是down时，即用户层执行ifconfig down命令时，执行ip_rt_flush，对路由表进行刷新
		ip_rt_flush(ptr);
	return NOTIFY_DONE;
}

struct notifier_block ip_rt_notifier={
	ip_rt_event,
	NULL,
	0
};

/*
 *	IP registers the packet type and then calls the subprotocol initialisers
 */

void ip_init(void)
{
	ip_packet_type.type=htons(ETH_P_IP);
	dev_add_pack(&ip_packet_type);//添加ip接收能力结构体

	/* So we flush routes when a device is downed */	
	//注册网卡设备通知事件
	register_netdevice_notifier(&ip_rt_notifier);
/*	ip_raw_init();
	ip_packet_init();
	ip_tcp_init();
	ip_udp_init();*/
}

